package com.harmontronics.hanma.paas.paas.controller;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.annotation.Log;
import com.harmontronics.hanma.paas.paas.annotation.TokenValidSkip;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.consts.BaseI18nKeyEnum;
import com.harmontronics.hanma.paas.paas.domain.tenant.entity.HmTenantApp;
import com.harmontronics.hanma.paas.paas.domain.tenant.entity.TenantAppTree;
import com.harmontronics.hanma.paas.paas.dto.input.HmTenantAppCreateInput;
import com.harmontronics.hanma.paas.paas.dto.input.HmTenantAppUpdateInput;
import com.harmontronics.hanma.paas.paas.dto.output.HmTenantAppResult;
import com.harmontronics.hanma.paas.paas.exception.ErrorKeyException;
import com.harmontronics.hanma.paas.paas.service.HmTenantAppExtService;
import com.harmontronics.hanma.paas.paas.service.IHmTenantAppService;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;
import com.harmontronics.hanma.paas.paas.utils.RequestUtils;
import com.harmontronics.hanma.paas.paas.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Arrays;
import java.util.List;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 租户-应用表
 *
 * @author cms-generator
 * @email cms-generator@harmontronics.com
 * @date 2021-03-12 10:21:19
 */
@RestController
@RequestMapping("/api/tenant/app")
@Api(tags = {"租户-应用表"})
public class HmTenantAppController {
    @Autowired
    private IHmTenantAppService hmTenantAppExtService;



    /**
     * 分页列表
     */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmTenantAppResult>> list(@ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
                                                            @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize,
                                                            @RequestParam(required = false) String appName, @RequestParam(required = false) String tenantName,
                                                            @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime,
                                                            @RequestParam(required = false, defaultValue = "0") int deleted, @RequestParam(required = false, defaultValue = "0") int released) {
        PageBase<HmTenantApp> hmTenantAppPageBase = hmTenantAppExtService.getList(pageNo, pageSize, appName, tenantName, startTime, endTime, deleted, released);
        PageBase<HmTenantAppResult> resultPageBase = PageBase.copy(hmTenantAppPageBase, HmTenantAppResult.class);
        return ResponseEntity.ok(resultPageBase);
    }

    /**
     * 分页列表
     */
    @GetMapping("/page/bin")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmTenantAppResult>> listBin(@ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
                                                               @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize,
                                                               @RequestParam(required = false) String appName) {
        PageBase<HmTenantApp> hmTenantAppPageBase = hmTenantAppExtService.getListBin(pageNo, pageSize, appName);
        PageBase<HmTenantAppResult> resultPageBase = PageBase.copy(hmTenantAppPageBase, HmTenantAppResult.class);
        return ResponseEntity.ok(resultPageBase);
    }

    /**
     * 获取所有app
     */
    @GetMapping("/tree")
    @ApiOperation("获取所有app")
    public ResponseEntity<List<TenantAppTree>> getTree(@RequestParam(required = false, defaultValue = "0") int deleted) {
        String token = RequestUtils.getToken();
        List<TenantAppTree> tree = hmTenantAppExtService.getAllApp(token, deleted);
        return ResponseEntity.ok(tree);
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseEntity<HmTenantAppResult> info(@RequestParam Long id) {
        HmTenantApp hmTenantApp = hmTenantAppExtService.findById(id);
        HmTenantAppResult result = BeanUtil.toBean(hmTenantApp, HmTenantAppResult.class);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    @Log("创建了APP")
    public ResponseEntity<String> save(@Valid @RequestBody HmTenantAppCreateInput hmTenantAppInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmTenantApp hmTenantApp = BeanUtil.toBean(hmTenantAppInput, HmTenantApp.class);
        hmTenantAppExtService.save(hmTenantApp, RequestUtils.getToken());

        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    @Log("修改了APP信息")
    public ResponseEntity<String> update(@Valid @RequestBody HmTenantAppUpdateInput hmTenantAppInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmTenantApp hmTenantApp = BeanUtil.toBean(hmTenantAppInput, HmTenantApp.class);
        String token = RequestUtils.getToken();
        hmTenantAppExtService.update(hmTenantApp, token);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    @Log("删除了APP")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
        String token = RequestUtils.getToken();
        hmTenantAppExtService.remove(Arrays.asList(ids), token);
        return ResponseEntity.ok();
    }

    /**
     * 保存文件夹
     */
    @PostMapping("/create-folder")
    @ApiOperation("保存文件夹")
    @Log("添加了文件夹")
    public ResponseEntity<String> saveFolder(@Valid @RequestBody HmTenantAppCreateInput hmTenantAppInput) {
        if (StringUtil.isBlank(hmTenantAppInput.getAppName())) {
            throw new ErrorKeyException(BaseI18nKeyEnum.REQUEST_PARAM_ERROR);
        }
        String token = RequestUtils.getToken();
        HmTenantApp hmTenantApp = BeanUtil.toBean(hmTenantAppInput, HmTenantApp.class);
        hmTenantAppExtService.saveFolder(hmTenantApp, token);

        return ResponseEntity.ok();
    }

    /**
     * 保存文件夹
     */
    @PutMapping("/update-folder")
    @ApiOperation("修改文件夹")
    @Log("修改了文件夹信息")
    public ResponseEntity<String> updateFolder(@Valid @RequestBody HmTenantAppUpdateInput hmTenantAppInput) {
        if (StringUtil.isBlank(hmTenantAppInput.getAppName())) {
            throw new ErrorKeyException(BaseI18nKeyEnum.REQUEST_PARAM_ERROR);
        }
        String token = RequestUtils.getToken();
        HmTenantApp hmTenantApp = BeanUtil.toBean(hmTenantAppInput, HmTenantApp.class);
        hmTenantAppExtService.updateFolder(hmTenantApp, token);

        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete-folder")
    @ApiOperation("删除文件夹")
    @Log("删除了文件夹")
    public ResponseEntity<String> deleteFolder(@RequestBody Long[] ids) {
        String token = RequestUtils.getToken();
        hmTenantAppExtService.removeFolder(Arrays.asList(ids), token);
        return ResponseEntity.ok();
    }

    @PutMapping("/recover")
    @ApiOperation("恢复")
    @Log("恢复了APP")
    public ResponseEntity<String> update(@RequestBody HmTenantAppUpdateInput hmTenantAppUpdateInput) {
        hmTenantAppExtService.recover(hmTenantAppUpdateInput.getId());
        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update-status")
    @ApiOperation("修改")
    @Log("修改了APP状态")
    public ResponseEntity<String> updateStatus(@Valid @RequestBody HmTenantAppUpdateInput hmTenantAppInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmTenantApp hmTenantApp = BeanUtil.toBean(hmTenantAppInput, HmTenantApp.class);
        hmTenantAppExtService.updateStatus(hmTenantApp);
        return ResponseEntity.ok();
    }

    @GetMapping("/info-unique")
    @ApiOperation("根据unique获取信息")
    @TokenValidSkip
    public ResponseEntity<HmTenantAppResult> getByUnique() {
        String appUnique = RequestUtils.getAppUnique();
        String tmp = appUnique.replace("hm_", "");
        HmTenantApp hmTenantApp = hmTenantAppExtService.findByUnique(tmp);
        HmTenantAppResult result = BeanUtil.toBean(hmTenantApp, HmTenantAppResult.class);
        return ResponseEntity.ok(result);
    }

}
